{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<form class="layui-form p-4">
	{if condition="$id eq 0"}
	<h3 class="pb-3">添加律师信息</h3>
	<table class="layui-table layui-table-form">
		<tr>
			<td class="layui-td-gray">律师姓名<font>*</font></td>
			<td>
				<input type="text" name="name" lay-verify="required" placeholder="请输入律师姓名" lay-reqText="请输入律师姓名" autocomplete="off" class="layui-input">
			</td>
			<td class="layui-td-gray">律师性别<font>*</font></td>
			<td>
				<input type="radio" name="sex" value="1" title="男">
				<input type="radio" name="sex" value="2" title="女">
			</td>
			<td class="layui-td-gray">初始密码</td>
			<td>
				{$reg_pwd}
				<input type="hidden" name="reg_pwd" value="{$reg_pwd}" lay-verify="required">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">手机号码<font>*</font></td>
			<td>
				<input type="text" name="mobile" lay-verify="required" placeholder="可使用手机号码登录" lay-reqText="请输入手机" autocomplete="off" class="layui-input">
			</td>
			<td class="layui-td-gray">执业城市</td>
			<td>
				<div class="layui-form-item" style="display: inline;">
					<div class="layui-input-inline">
					  <select name="provinceid" lay-filter="provinceid">
						<option value="">请选择省份</option>
						<!-- 省份选项 -->
					  </select>
					</div>
		
					<div class="layui-input-inline">
					  <select name="cityid" lay-filter="cityid">
						<option value="">请选择城市</option>
						<!-- 城市选项 -->
					  </select>
					</div>
				  </div>
			</td>
			<td rowspan="3" class="layui-td-gray">头像<br /><span style="font-size: 12px;">(如若不上传<br />系统将自动生成)</span></td>
			<td rowspan="3" valign="top" style="width: 100px;">
				<div class="layui-upload" style="width: 100px;">
					<div class="layui-upload-list" id="demo1" style="width: 100%; height:100px; overflow: hidden;">
						<img src="/static/home/images/nonepic600x600.jpg" height="100" style="max-width: 100%; width: 100%;" />
						<input type="hidden" name="thumb" value="">
					</div>
					<button type="button" class="layui-btn layui-btn-normal" style="width: 100%;" id="test1">上传头像</button>
				</div>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">所在律所<font>*</font></td>
			<td>
				<select name="firmid" lay-verify="required" lay-reqText="请选择所属律所">
					<option value="">请选择所属律所</option>
					{volist name="firm" id="v"}
						<option value="{$v.id}">{$v.title}</option>
					{/volist}
				</select>
			</td>
			<td class="layui-td-gray">律师类型<font>*</font></td>
			<td>
				<input type="radio" name="type" value="3" title="实习律师">
				<input type="radio" name="type" value="2" title="试用律师">
				<input type="radio" name="type" value="1" title="合作律师">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">入库日期<font>*</font></td>
			<td>
				<input type="text" id="entry_time" name="entry_time" readonly lay-verify="required" placeholder="请选择入库日期" lay-reqText="请选择入职日期" autocomplete="off" class="layui-input">
			</td>
			<td class="layui-td-gray">账号状态</td>
			<td>
				<input type="radio" name="status" value="0" title="禁止登录" checked>
				<input type="radio" name="status" value="1" title="正常">
				<input type="radio" name="status" value="2" title="离职">
			</td>
			
		</tr>
		<tr>
			<td class="layui-td-gray">律师简介</td>
			<td colspan="7">
				<textarea name="desc" placeholder="请输入律师简介" class="layui-textarea"></textarea>
			</td>
		</tr>
	</table>
	{else/}
	<h3 class="pb-3">编辑律师信息</h3>
	<table class="layui-table layui-table-form">
		<tr>
			<td class="layui-td-gray">律师姓名<font>*</font></td>
			<td>
				<input type="text" name="name" value="{$detail.name}" lay-verify="required" placeholder="请输入律师姓名" lay-reqText="请输入员工姓名" autocomplete="off" class="layui-input">
			</td>
			<td class="layui-td-gray">登录账号</td>
			<td>
				{$detail.username}
			</td>
			<td class="layui-td-gray">初始密码</td>
			<td>
				{empty name="$detail.reg_pwd"}
					密码已重置
				{else/}
					{$detail.reg_pwd}
				{/empty}
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">手机号码<font>*</font></td>
			<td>
				<input type="text" name="mobile" value="{$detail.mobile}" lay-verify="required" placeholder="可使用手机号码登录" lay-reqText="请输入手机" autocomplete="off" class="layui-input">
			</td>
			<td class="layui-td-gray">执业城市</td>
			<td>
				<div class="layui-form-item" style="display: inline;">
					<div class="layui-input-inline">
					  <select name="provinceid" lay-filter="provinceid">
						<option value="">请选择省份</option>
						<!-- 省份选项 -->
					  </select>
					</div>
		
					<div class="layui-input-inline">
					  <select name="cityid" lay-filter="cityid">
						<option value="">请选择城市</option>
						<!-- 城市选项 -->
					  </select>
					</div>
				  </div>
			</td>
			<td rowspan="3" class="layui-td-gray">头像<br /><span style="font-size: 12px;">(如若不上传<br />系统将自动生成)</span></td>
			<td rowspan="3" valign="top" style="width: 100px;">
				<div class="layui-upload" style="width: 100px;">
					<div class="layui-upload-list" id="demo1" style="width: 100%; height:100px; overflow: hidden;">
						<img src="{$detail.thumb}" height="100" style="max-width: 100%; width: 100%;" />
						<input type="hidden" name="thumb" value="{$detail.thumb}">
					</div>
					<button type="button" class="layui-btn layui-btn-normal" style="width: 100%;" id="test1">上传头像</button>
				</div>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">所在律所<font>*</font></td>
			<td>
				<select name="firmid" lay-verify="required" lay-reqText="请选择所属律所">
					<option value="">请选择所属律所</option>
					{volist name="firm" id="v"}
						<option value="{$v.id}" {eq name="$v.id" value="$detail.firmid"}selected{/eq}>{$v.title}</option>
					{/volist}
				</select>
			</td>
			<td class="layui-td-gray">律师类型<font>*</font></td>
			<td>
				<input type="radio" name="type" value="3" title="实习律师" {eq name="$detail.type" value="3"}checked{/eq}>
				<input type="radio" name="type" value="2" title="试用律师" {eq name="$detail.type" value="2"}checked{/eq}>
				<input type="radio" name="type" value="1" title="合作律师" {eq name="$detail.type" value="1"}checked{/eq}>
			</td>

		</tr>
		<tr>
			<td class="layui-td-gray">入库日期<font>*</font></td>
			<td>
				<input type="text" id="entry_time" value="{$detail.entry_time | date='Y-m-d'}" name="entry_time" readonly lay-verify="required" placeholder="请选择入职日期" lay-reqText="请选择入职日期" autocomplete="off" class="layui-input">
			</td>
			<td class="layui-td-gray">账号状态</td>
			<td>
				{eq name="$detail.status" value="0"}<span style="color:#FF5722">禁止登录</span>{/eq}
				{eq name="$detail.status" value="1"}<span style="color:#009688">正常</span>{/eq}
				{eq name="$detail.status" value="2"}<span style="color:#FFB800">已离职</span>{/eq}
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">律师性别<font>*</font></td>
			<td colspan="5">
				<input type="radio" name="sex" value="1" title="男" {eq name="$detail.sex" value="1"}checked{/eq}>
				<input type="radio" name="sex" value="2" title="女" {eq name="$detail.sex" value="2"}checked{/eq}>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">律师简介</td>
			<td colspan="7">
				<textarea name="desc" placeholder="请输入员工简介" class="layui-textarea">{$detail.desc|default=''}</textarea>
			</td>
		</tr>
	</table>
	{/if}

	<div class="py-3">
		<input type="hidden" value="{$id}" name="id">
		<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">立即提交</button>
		<button type="reset" class="layui-btn layui-btn-primary">重置</button>
	</div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	const moduleInit = ['tool'];

	function gouguInit() {
		var form = layui.form,tool=layui.tool,laydate = layui.laydate,upload = layui.upload;
		//日期选择
		laydate.render({
			elem: '#entry_time',
			max:7,
			showBottom: false
		});
		// 头像上传
		var uploadInst = upload.render({
			elem: "#test1",
			url: "/api/index/upload",
			done: function (res) {
				layer.msg(res.msg);
				if (res.code == 0) {
					// 上传成功
					$('#demo1 input').attr('value', res.data.filepath);
					$('#demo1 img').attr('src', res.data.filepath);
				}
			},
			error: function() {
				layer.msg('头像上传失败，请稍后重试');
			}
		});

		//监听提交
		form.on('submit(webform)', function(data){
			if(!data.field.sex){
				layer.msg('请选择员工性别');
				return false;
			}
			let callback = function (e) {
				layer.msg(e.msg);
				if (e.code == 0) {
					tool.sideClose(1000);
				}
			}
			tool.post("/lawyer/lawyer/add", data.field, callback);
			return false;
		});

		var layer = layui.layer;
		var isEditMode = '{$id}' != '0';
		var provinceId = isEditMode ? '{$detail.provinceid}' : '';
		var cityId = isEditMode ? '{$detail.cityid}' : '';	


		// 预加载省选项
		loadRegionOptions('provinceid', '/home/api/regionProvinces', { selectedId: provinceId });

		// 预加载市选项
		if (provinceId) {
			loadRegionOptions('cityid', '/home/api/regionCitys', { provinceId: provinceId, selectedId: cityId });
		}

		// 监听省份选择
		form.on('select(provinceid)', function(data) {
			var provinceId = data.value;
			if (provinceId) {
				// 加载城市选项
				loadRegionOptions('cityid', '/home/api/regionCitys', { provinceId: provinceId });
			} else {
				$('select[name="cityid"]').html('<option value="">请选择城市</option>');
				$('select[name="distid"]').html('<option value="">请选择区/县</option>');
				form.render();
			}
		});

		// 优化后的 loadRegionOptions 函数
		function loadRegionOptions(selectName, url, params = {}) {
			$.get(url, params)
			.done(function(data) {
					var redata = data.data;
					var html = '<option value="">请选择</option>';
					for (var i = 0; i < redata.length; i++) {
						var selected = params.selectedId && redata[i].id == params.selectedId ? 'selected' : '';
						html += `<option value="${redata[i].id}" ${selected}>${redata[i].name}</option>`;
					}
					$(`select[name="${selectName}"]`).html(html);
					form.render();
				})
			.fail(function(jqXHR, textStatus, errorThrown) {
					layer.msg(`加载地区选项失败: ${textStatus} - ${errorThrown}`);
				});
		}
	}

</script>
{/block}
<!-- /脚本 -->